package com.atguigu.e_threadstart4;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo1 {
    public static void main(String[] args) {
        //1. 创建线程池对象
        ExecutorService pool = Executors.newCachedThreadPool();//不指定线程数量上限
        System.out.println("pool = " + pool);

        //2. 给线程池提交任务
        for (int i = 1; i <= 100; i++) {
            //实时final : 在JDK8之后,如果是局部变量在匿名内部类中使用时,JVM会自动给此变量加 final 关键字
            /*final*/ int j = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    //Thread.currentThread().getName() : 获取线程对象的名称
                    System.out.println(Thread.currentThread().getName() + " 执行了第 " + j + " 个任务 !");
                }
            });
        }

        //关闭线程池
        pool.shutdown();
    }
}
